# base image
FROM node:20.11.1-alpine3.19 AS base
LABEL maintainer="ggbahck@gmail.com"

# if you are located in China, you can use aliyun mirror to speed up
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

RUN apk add --no-cache tzdata



# install packages
FROM base as packages

WORKDIR /app/web

COPY package.json .
COPY package-lock.json .
COPY .env.local .

# if you are located in China, you can use taobao registry to speed up
RUN npm install --frozen-lockfile --registry https://registry.npmmirror.com/

# RUN npm install --frozen-lockfile

# build resources
FROM base as builder
WORKDIR /app/web
COPY --from=packages /app/web/ .
COPY . .

RUN npm run build

# production stage
FROM base as production

ENV NODE_ENV production
ENV EDITION SELF_HOSTED
ENV DEPLOY_ENV PRODUCTION
# Deployment used by `npx convex dev`
ENV CONVEX_DEPLOYMENT dev:successful-gnu-987 
# team: ishow520, project: notion-clone-2f3b0

ENV NEXT_PUBLIC_CONVEX_URL https://successful-gnu-987.convex.cloud
# https://dashboard.clerk.com/apps/app_2gLg26oIr0FnFn2uHVwtWFTlh81/instances/ins_2gLg2AwfeZ1lmM0ntbFwcb7trez
ENV NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY pk_test_ZmFpdGhmdWwtYnVjay0yOC5jbGVyay5hY2NvdW50cy5kZXYk
ENV CLERK_SECRET_KEY sk_test_dLKbo0VvR8k5cFY9EGRVX6WldDRxEKVVVs1NuQLLQi

# edgeStore.dev
ENV EDGE_STORE_ACCESS_KEY qvKiuFcDZ55OhIV9oJ3bpuNi0IxkS6By
ENV EDGE_STORE_SECRET_KEY pm1zGO1LcklwWa7iCvyUk5pNLUWsRICGhsIsY8NeLM5KEoRY
ENV PORT 3000



# set timezone
ENV TZ UTC
RUN ln -s /usr/share/zoneinfo/${TZ} /etc/localtime \
    && echo ${TZ} > /etc/timezone

# global runtime packages
RUN npm install -g convex \
    && npm install -g pm2 \
    && npm cache clean --force 

WORKDIR /app/web
COPY --from=builder /app/web/public ./public
COPY --from=builder /app/web/.next/standalone ./
COPY --from=builder /app/web/.next/static ./.next/static

COPY docker/pm2.json ./pm2.json
COPY docker/entrypoint.sh ./entrypoint.sh

ARG COMMIT_SHA
ENV COMMIT_SHA ${COMMIT_SHA}

EXPOSE 3000
ENTRYPOINT ["/bin/sh", "./entrypoint.sh"]
